package com.morgan.recyclerviewdemo.itemdecoration

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.ColorInt
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

/**
 *description：
 *<br>author：caowugao
 *<br>time：2020/7/5 0005 17:45
 */
class GridItemDecoration(var mNum : Int, @RecyclerView.Orientation var  mOrientation : Int) : RecyclerView.ItemDecoration() {
    private @ColorInt var mHorizontalColor : Int = 0
    private var mHorizontalDrawable : Drawable? = null
    private var mHorizontalSize : Int = 0
    private var mHorizontalMargin : Rect? = null

    private @ColorInt var mVerticalColor : Int = 0
    private var mVerticalDrawable : Drawable? = null
    private var mVerticalSize : Int = 0
    private var mVerticalMargin : Rect? = null
    var mAgainstLeftSide : Boolean = false
    var mAgainstRightSide : Boolean = false

    private val mPaint : Paint
    init {
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    }

    constructor(builder : Builder) : this(builder.mNum, builder.mOrientation) {
        mHorizontalColor = builder.mHorizontalColor
        mHorizontalDrawable = builder.mHorizontalDrawable
        mHorizontalSize = builder.mHorizontalSize
        mHorizontalMargin = builder.mHorizontalMargin

        mVerticalColor = builder.mVerticalColor
        mVerticalDrawable = builder.mVerticalDrawable
        mVerticalSize = builder.mVerticalSize
        mVerticalMargin = builder.mVerticalMargin
        mAgainstLeftSide = builder.mAgainstLeftSide
        mAgainstRightSide = builder.mAgainstRightSide

    }

    class Builder(var mNum : Int, @RecyclerView.Orientation var  mOrientation : Int) {

        @ColorInt var mHorizontalColor : Int = 0
        var mHorizontalDrawable : Drawable? = null
        var mHorizontalSize : Int = 0
        var mHorizontalMargin : Rect? = null

        @ColorInt var mVerticalColor : Int = 0
         var mVerticalDrawable : Drawable? = null
        var mVerticalSize : Int = 0
        var mVerticalMargin : Rect? = null

        var mAgainstLeftSide : Boolean = false
        var mAgainstRightSide : Boolean = false

        fun horizontalColor(@ColorInt color : Int) : Builder{
            this.mHorizontalColor = color
            return this
        }

        fun horizontalDrawable(drawable : Drawable) : Builder{
            this.mHorizontalDrawable = drawable
            return this
        }

        fun horizontalSize(size : Int) : Builder {
            this.mHorizontalSize = size
            return this
        }

        fun horizontaMargin(left: Int, top: Int, right: Int, bottom: Int) : Builder{
            if (mHorizontalMargin == null) {
                mHorizontalMargin = Rect(left, top, right, bottom)
            } else {
                mHorizontalMargin!!.set(left, top, right, bottom)
            }
            return this
        }

        fun verticalColor(@ColorInt color : Int) : Builder{
            this.mVerticalColor = color
            return this
        }

        fun verticalDrawable(drawable : Drawable) : Builder{
            this.mVerticalDrawable = drawable
            return this
        }

        fun verticalSize(size : Int) : Builder {
            this.mVerticalSize = size
            return this
        }

        fun verticalMargin(left: Int, top: Int, right: Int, bottom: Int) : Builder{
            if (mVerticalMargin == null) {
                mVerticalMargin = Rect(left, top, right, bottom)
            } else {
                mVerticalMargin!!.set(left, top, right, bottom)
            }

            return this
        }

        fun againstLeftSide(value : Boolean) : Builder{
            mAgainstLeftSide = value
            return this
        }

        fun againstRightSide(value : Boolean) : Builder{
            mAgainstRightSide = value
            return this
        }

        fun build() : GridItemDecoration{
            return GridItemDecoration(this)
        }
    }

    private fun getSpanCount(parent: RecyclerView): Int {
        // 列数
        var spanCount = -1
        val layoutManager = parent.layoutManager
        if (layoutManager is GridLayoutManager) {
            spanCount = layoutManager.spanCount
        } else if (layoutManager is StaggeredGridLayoutManager) {
            spanCount = layoutManager.spanCount
        }
        return spanCount
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        parent.adapter ?: return

        when (mOrientation) {
            RecyclerView.HORIZONTAL -> setItemOffsetsHorizontal(outRect, view, parent, state)
            RecyclerView.VERTICAL -> setItemOffsetsVertical(outRect, view, parent, state)
        }
    }

    private fun setItemOffsetsVertical(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {

        var verticalMarginLeft = 0
        var verticalMarginTop = 0
        var verticalMarginRight = 0
        var verticalMarginBottom = 0

        mVerticalMargin?.apply {
            verticalMarginLeft = left
            verticalMarginTop = top
            verticalMarginRight = right
            verticalMarginBottom = bottom
        }

        outRect.top = 0
        outRect.bottom = 0

        val position = parent.getChildAdapterPosition(view)
        val childParams = view.layoutParams as RecyclerView.LayoutParams
        val itemCount = parent.adapter!!.itemCount
        outRect.left = verticalMarginLeft + mVerticalSize + verticalMarginRight

        if (mAgainstLeftSide && position % mNum == 0) {//左边是否贴边
            outRect.left = 0
        } else if (mAgainstRightSide && position%(mNum - 1) == 0) {//右边贴边
            outRect.right = 0
        } else if (position%(mNum - 1) == 0) {//最右边不贴边
            outRect.right = outRect.left
        }



    }

    private fun setItemOffsetsHorizontal(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {

    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
    }
}